package com.wzy.vx.controller;

import com.wzy.vx.DTO.OrderDTO;
import com.wzy.vx.enums.ResuletEnum;
import com.wzy.vx.exception.SellException;
import com.wzy.vx.service.OrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * @ProjectName: vx
 * @Package: com.wzy.vx.controller
 * @ClassName: SellerOrderController
 * @Author: Administrator
 * @Description: ${卖家端}
 * @Date: 2019/3/18 13:53
 * @Version: 1.0
 */
@Controller
@RequestMapping("/seller/order")
public class SellerOrderController {

    private final static Logger log = LoggerFactory.getLogger(SellerOrderController.class);

    @Autowired
    private OrderService orderService;
    /**
     * 订单列表
     * */
    @GetMapping("/list")
    public ModelAndView list(@RequestParam(value = "page",defaultValue = "1") Integer page,
                             @RequestParam(value = "size",defaultValue = "10") Integer size,
                             Map<String,Object> map){
        PageRequest request = new PageRequest(page-1,size);
        Page<OrderDTO> orderDTOPage = orderService.findAllList(request);
        map.put("orderDTOPage",orderDTOPage);
        map.put("currentPage",page);
        map.put("size",size);
        return  new ModelAndView("order/list",map);
    }

    /**
     * 取消订单
     * */
    @GetMapping("/cancel")
    public ModelAndView cancel(@RequestParam(value = "orderId") String orderId,Map<String,Object> map){
        try {
            OrderDTO orderDTO = orderService.findOrder(orderId);
            orderService.cannel(orderDTO);
        }
        catch (SellException e){
                log.error("[买家取消订单]发生异常");
                map.put("msg",e.getMessage());
                map.put("url","/sell/seller/order/list");
                return new ModelAndView("common/error",map);
        }
            map.put("msg", ResuletEnum.ORDER_CANCEL_SUCCESS.getMsg());
            map.put("url", "/sell/seller/order/list");
            return new ModelAndView("common/success",map);
    }

    /**
     * 查询订单
     * */
    @GetMapping("/detail")
    public ModelAndView detail(@RequestParam(value = "orderId") String orderId,Map<String,Object> map){
        OrderDTO orderDTO1 = new OrderDTO();
        try {
             orderDTO1 = orderService.findOrder(orderId);
        } catch (SellException e){
            log.error("[卖家查询订单详情]发生异常{}",e);
            map.put("msg",e.getMessage());
            map.put("url","/sell/seller/order/list");
            return new ModelAndView("common/error",map);
        }
        map.put("orderDTO",orderDTO1);
        map.put("msg", ResuletEnum.ORDER_CANCEL_SUCCESS.getMsg());
        map.put("url", "/sell/seller/order/list");
        return new ModelAndView("order/detail",map);
    }

    @GetMapping("/finish")
    public ModelAndView finished(@RequestParam("orderId") String orderId,
                                 Map<String, Object> map) {
        try {
            OrderDTO orderDTO = orderService.findOrder(orderId);
            orderService.finish(orderDTO);
        } catch (SellException e) {
            log.error("【卖家端完结订单】发生异常{}", e);
            map.put("msg", e.getMessage());
            map.put("url", "/sell/seller/order/list");
            return new ModelAndView("common/error", map);
        }
        map.put("msg", ResuletEnum.ORDER_FINISH_SUCCESS.getMsg());
        map.put("url", "/sell/seller/order/list");
        return new ModelAndView("common/success");
    }

}
